package com.csg.controller;


import com.alibaba.fastjson.JSON;
import com.csg.annotation.RequestUser;
import com.csg.entity.Resources;
import com.csg.model.RequestSubject;
import com.csg.model.Result;
import com.csg.model.StatusCode;
import com.csg.model.vo.MenuVO;
import com.csg.service.IResourcesService;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 菜单资源表 前端控制器
 * </p>
 *
 * @author George Chan
 * @since 2020-02-06
 */
@RestController
@RequestMapping("/api/system/resources/")
public class ResourcesController {
    private static final Logger LOGGER = LoggerFactory.getLogger(RestController.class);
    private final IResourcesService resourcesService;

    @Autowired
    public ResourcesController(IResourcesService resourcesService) {
        this.resourcesService = resourcesService;
    }

    /**
     * 获取所有菜单列表
     * @param subject 当前登录用户
     * @return 菜单数据集合
     */
    @RequestMapping(value = "menulist", method = RequestMethod.GET)
    public Result menuList(@RequestUser RequestSubject subject) {
        LOGGER.info("当前登录用户===》 {}", JSON.toJSONString(subject));
        List<MenuVO> menuVOList = resourcesService.listMenus();
        return new Result(true, StatusCode.OK, "查询成功！", menuVOList);
    }

    /**
     * 添加菜单
     * @param resources 数据实体
     * @return 操作结果
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public Result addResource(@RequestBody Resources resources) {
        if (ObjectUtils.isEmpty(resources)) {
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
        resources.setCreateTime(LocalDateTime.now());
        boolean save = resourcesService.save(resources);
        if (!save) {
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
        return new Result(true, StatusCode.OK, "添加成功");
    }
}
